package cn.fancy.sdk.baseview;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import cn.fancy.sdk.common.utils.DimensionUtil;
import cn.fancy.sdk.common.utils.Logger;

public abstract class BaseView extends LinearLayout implements
		View.OnClickListener {

	public View mainView;
	public String viewId;
	LayoutInflater inflater;

	// for term view
	public void setMainViewAsWebView(WebView view) {
		this.setOrientation(LinearLayout.VERTICAL);

		mainView = view;

		this.addView(mainView);
	}

	// for BBS view
	public void setMainViewAsWebView(ProgressBar pbar, WebView view) {

		this.setOrientation(LinearLayout.VERTICAL);

		this.addView(
				pbar,
				new LinearLayout.LayoutParams(
						LinearLayout.LayoutParams.MATCH_PARENT, DimensionUtil
								.dip2px(getContext(), 2)));

		mainView = view;

		this.addView(mainView);
	}

	public BaseView(Context context, int LayoutId) {
		super(context);
		viewId = null;
		inflater = LayoutInflater.from(context);
		mainView = inflater.inflate(LayoutId, null);
		mainView.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));

		this.addView(mainView, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));

	}

	public BaseView(Context context, int LayoutId, int width_in_px,
			int height_in_px, int view_page) {

		super(context);

		this.setOrientation(LinearLayout.HORIZONTAL);

		this.setGravity(Gravity.CENTER | Gravity.CENTER_VERTICAL
				| Gravity.CENTER_HORIZONTAL);

		inflater = LayoutInflater.from(context);

		mainView = inflater.inflate(LayoutId, null);

		mainView.setLayoutParams(new LinearLayout.LayoutParams(width_in_px,
				height_in_px));
		// mainView.setLayoutParams(new
		// LinearLayout.LayoutParams(1000,height_in_px));

		int w = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);
		int h = View.MeasureSpec.makeMeasureSpec(0,
				View.MeasureSpec.UNSPECIFIED);

		mainView.measure(w, h);

		int vheight = mainView.getMeasuredHeight();
		int width = mainView.getMeasuredWidth();

		WindowManager wm = (WindowManager) this.getContext().getSystemService(
				Context.WINDOW_SERVICE);
		DisplayMetrics dm = new DisplayMetrics();
		wm.getDefaultDisplay().getMetrics(dm);
		int scrHeight = dm.heightPixels;

		Logger.e("real view data", width_in_px + "   .....   " + height_in_px);

		Logger.e("measure data", width + "   :   " + vheight);

		if (scrHeight > vheight) {
			if (view_page == 0) {
				this.setPadding(0, (scrHeight - vheight) / 2, 0, 0);
			} else if (view_page == 1) {
				this.setPadding(0, (scrHeight - height_in_px) / 2, 0, 0);
			}
		}

		Logger.print_red("loginView - width=" + width + ",height=" + vheight);

		this.addView(mainView);

	}

	public BaseView(Context context, View v) {
		super(context);
		mainView = v;
		mainView.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));

		this.addView(mainView, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));
	}

	public BaseView(Context context, View header, View listview) {
		super(context);

		this.setOrientation(LinearLayout.VERTICAL);

		this.addView(header, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.WRAP_CONTENT));

		mainView = listview;

		mainView.setLayoutParams(new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));

		this.addView(mainView, new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT,
				LinearLayout.LayoutParams.MATCH_PARENT));
	}

	public BaseView(Context context) {

		super(context);

	}

	public void stopSyncTask(boolean stop) {

	}

}
